package com.zhiguang.cms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiguang.cms.dao.UserMapper;
import com.zhiguang.cms.entity.User;
import com.zhiguang.cms.service.UserService;
import com.zhiguang.cms.utils.CmsContant;
import com.zhiguang.cms.utils.CmsUtils;

/**
 * 
 * @author zhuzg
 *
 */
@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	UserMapper userMapper;

	@Override
	public int register(User user) {
		// TODO Auto-generated method stub
		user.setPassword(CmsUtils.md5Encrytype(user.getPassword(), user.getUsername()));
		return userMapper.add(user);
		
	}

	@Override
	public User findByName(String username) {
		// TODO Auto-generated method stub
		return userMapper.findByName(username);
	}

	/**
	 * 登录
	 */
	@Override
	public User login(User user) {
		// TODO Auto-generated method stub
		User loginUser = findByName(user.getUsername());
		String pwdmd5 = CmsUtils.md5Encrytype(user.getPassword(), user.getUsername());
		// 数据库存在该名字的用户 而且加密后的密码 与现有用户的密码一致
		if(loginUser!=null && pwdmd5.equals(loginUser.getPassword()))
			return loginUser;
		
		//登录失败
		return null;
			
	}

	@Override
	public PageInfo<User> list(int page) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page,CmsContant.PAGE_SIZE);
		return new PageInfo<User>(userMapper.list());
		
	}

	@Override
	public int lock(int userid, int status) {
		// TODO Auto-generated method stub
		return userMapper.updateLocked(userid,status);
	}

}
